package com.shop.cereshop.agent.controller.product;
import com.shop.cereshop.agent.page.product.ProductDetailVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.param.product.ProductListDTO;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.agent.page.product.ProductVO;
import com.shop.cereshop.agent.service.product.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RequestMapping("product")
@RestController
@Api(value = "代理商商品接口", tags = "代理商商品接口")
public class AgentProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("detail")
    @ApiOperation(value = "获取商品详情接口")
    public Result<ProductDetailVO> detail(@RequestParam("productId") @ApiParam(value = "商品id", required = true) Long productId,
                                          HttpServletRequest request) {
        CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
        if (user == null) {
            return Result.unauthorized();
        }
        ProductDetailVO result = productService.getProductDetail(productId, user.getAgentUserId());
        return Result.success(result);
    }

    @GetMapping("list")
    @ApiOperation(value = "获取商品列表接口")
    public Result<List<ProductVO>> getProducts(@ModelAttribute @Valid ProductListDTO param, HttpServletRequest request) {
        CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
        if (user == null) {
            return Result.unauthorized();
        }
        param.setAgentUserId(user.getAgentUserId());
        List<ProductVO> productVOList = productService.getProducts(param);
        return Result.success(productVOList);
    }
}
